<?php

	/*
	 * The TEXT main page
	 * Handles publicity text messages (creating, delete)
	 *
	 * Variables:
	 *
	 * Returns: HTML page
	 *
	 */


	$script = '
	function delfiltword(id){
		if(confirm("Are you sure you want to remove this word from the filter?")){
			$.get("'.$url_prefix.'ajax/filterworddelete/&id="+id, function(data){
				$("#filtword"+id).fadeOut();
			});
		}
	}

	function edittext(id){
		$.get("'.$url_prefix.'ajax/edittext/&id="+id, function(data){
			$(".editbox").html(data);
			$(".editbox").fadeIn("fast");
		});
	}
	function edittextclose(){
		$(".editbox").fadeOut("fast");
	}
	function edittextsave(){
		var tid = $(".editbox #id").val();
		var ttxt = $(".editbox #editbox_textarea").val();
		var tdt  = $(".editbox #st").val();
		$.post("'.$url_prefix.'ajax/savetext/", { id: tid, txt: ttxt, datetime: tdt }, function(data){
			if(tid > 0){
				$("#tdsms"+tid).html(data);
				$("#tdtsms"+tid).html(tdt.substr(5));
			}else{
//				$("#lowbar").before("<tr><td id=\"txtstatus0.png\" /></td><td id=\"tdtsms"+data+"\">"+tdt.substr(5)+"</td><td id=\"tdsms"+data+"\">"+ttxt+"</td><td><a class=\"fb\" href=\"javascript:edittext("+data+")\"><img src=\"'.$url_prefix.'images/page_white_edit.png\" /></a><a class=\"fb\" href=\"javascript:deltext("+data+")\"><img src=\"'.$url_prefix.'images/page_white_delete.png\" /></a></td></tr>");
				loadtextlist();
			}
			edittextclose();
		});
	}
	function addtext(){
		edittext(0);
	}
	function deltext(id){
		var temp = $("#tdsms"+id).text();
		if(confirm("Are you sure you want to remove the text \'"+temp+"\'?")){
			$.get("'.$url_prefix.'ajax/deletetext/", { id: id }, function(data){
				if(data == "ok"){
					$("#tdsms"+id).parent().slideUp();
				}else{
					alert(data);
				}
			});
		}
	}

	function loadstats(){
		$("#stats").parent().css("background","url('.$url_prefix.'/images/loadinv.gif) bottom right no-repeat");
		$("#stats").load("'.$url_prefix.'/ajax/loadstats/");
		$("#stats").parent().css("background","none");
	}

	$(document).ready(function(){
		$(".listdiv").hover(
			function(){ $(this).addClass("listdiv_hover"); },
			function(){ $(this).removeClass("listdiv_hover"); }
		);
		setInterval("loadstats()", 5000);
		loadtextlist();
	});




		var TXTmaxID = 0;
		var TXTminID = 9999999;



		function loadtextlist(){
			$("#smsloadlistindicator").css("display","inline");
			request = $.ajax({
				url: "'.$url_prefix.'ajax/loadtextlist/&id="+TXTmaxID,
				processData: false,
				success: function(xml){
//					alert("'.$url_prefix.'ajax/loadtextlist/&id="+TXTmaxID);
					$(request.responseXML).find("text").each(function(){
						$("#topbar").after("<tr><td id=\"txtstatus"+$(this).attr("id")+"\"><img src=\"'.$url_prefix.'images/status"+$(this).attr("status")+".png\" /></td><td id=\"tdtsms"+$(this).attr("id")+"\">"+$(this).attr("dt")+"</td><td id=\"tdsms"+$(this).attr("id")+"\">"+$(this).text()+"</td><td style=\"text-align:right\"><a class=\"fb\" href=\"javascript:edittext("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_edit.png\" /></a><a class=\"fb\"  href=\"javascript:deltext("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_delete.png\" /></a></td></tr>");
						TXTmaxID = Math.max(TXTmaxID, $(this).attr("id"));
						if($(this).attr("id") > 0){
							TXTminID = Math.min(TXTminID, $(this).attr("id"));
						}
					});

					$(request.responseXML).find("status").each(function(){
						$("#txtstatus"+$(this).attr("id")).html("<img src=\"'.$url_prefix.'images/status"+$(this).attr("status")+".png\" />");
					});


					if($(request.responseXML).find("text").length > 0){
						$("#smsinnerlist").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
					}

					$(".smslist tr").hover(
						function(){$(this).addClass("stripelight");},
						function(){$(this).removeClass("stripelight");}
					);
					$("#smsloadlistindicator").css("display","none");
					setTimeout("loadtextlist()", 2000);

					makestatuslinks();
				}
			});

		}

		function makestatuslinks(){
			$("img[src$=\'status2.png\']").hover(
				function(){ this.src="'.$url_prefix.'images/statusR.png"; },
				function(){ this.src="'.$url_prefix.'images/status2.png"; }
			);
			$("img[src$=\'status2.png\']").click(function(){
				if(confirm("Are you sure you want this text to appear again?")){
					$.get("'.$url_prefix.'ajax/textreset/&id="+$(this).parent().attr("id").substr(9));
					this.src="'.$url_prefix.'images/status3.png";
				}
			});
			$("img[src$=\'status1.png\']").hover(
				function(){ this.src="'.$url_prefix.'images/statusR.png"; },
				function(){ this.src="'.$url_prefix.'images/status1.png"; }
			);
			$("img[src$=\'status1.png\']").click(function(){
				if(confirm("Are you sure you want this text to appear again?")){
					$.get("'.$url_prefix.'ajax/textreset/&id="+$(this).parent().attr("id").substr(9));
					this.src="'.$url_prefix.'images/status3.png";
				}
			});


		}

	';



	htmlheader($script);

	echo '

	<div id="lpart">
	';


	echo '
		<h3>Statistics</h3>
		<div class="bigboxcover" style=""><div id="stats" style="margin: 3px;">
		</div></div>
	</div>
	';


	echo '
		<div id="rpart">
			<table class="smslistcover striped" style="width:100%">
				<tr style="height:30px;"><th>Texts List</th><th style="width:25px; text-align:center"><img src="'.$url_prefix.'/images/load.gif" style="" id="smsloadlistindicator" /></th></tr>
				<tr id="topbarx">
					<th colspan="2"><a href="javascript:addtext()" style="float:right">+ add text</a>Message</th>
				</tr>
				<tr><td colspan="2" style="padding:0px;">
				<div id="smsinnerlist" style="height:400px; overflow:hidden">
					<table class="smslist" style="width:100%">
					<tr id="topbar" style="display: none; height:1px; font-size:1px;">
						<th style="width: 20px;"></th>
						<th style="width: 60px;"></th>
						<th></th>
						<th style="width: 40px;"></th>
					</tr>
	';



	echo '
			<tr id="lowbar"><td style="width: 20px;"></td><td style="font-size: 1px; width: 60px">&nbsp;</td><td style="font-size: 1px;">&nbsp;</td><td style="width:40px;"></td></tr>
			</table>
			</td></tr></table>
		</div>

		<div class="editbox" style="height: 130px;"></div>
	';



	htmlfooter();
?>